package us.icitap.commandcenter.ui.operator;

import java.util.List;
import java.util.logging.Level;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.AppUser;
import us.icitap.commandcenter.business.Result;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.CallGradeHistory;
import us.icitap.commandcenter.business.ejb.persistence.CallReason;
import us.icitap.commandcenter.business.ejb.persistence.CallStatusEnum;
import us.icitap.commandcenter.business.ejb.persistence.CommandCenter;
import us.icitap.commandcenter.business.ejb.persistence.Dispatcher;
import us.icitap.commandcenter.business.ejb.persistence.Grade;
import us.icitap.commandcenter.listeners.NewCallListener;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.ConfirmDialogAction;
import us.icitap.commandcenter.ui.custom.CustButton;
import us.icitap.commandcenter.ui.custom.CustComboBox;
import us.icitap.commandcenter.ui.custom.CustConfirmDialog;
import us.icitap.commandcenter.ui.custom.CustFormLayout;
import us.icitap.commandcenter.ui.custom.CustSaveButton;
import us.icitap.commandcenter.ui.custom.CustTextArea;
import us.icitap.commandcenter.ui.custom.CustTextField;
import us.icitap.commandcenter.ui.custom.CustVerticalLayout;
import us.icitap.commandcenter.utils.ProjectLists;

import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.validator.CompositeValidator.CombinationMode;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.themes.Runo;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class CallForm extends CustVerticalLayout implements ClickListener{


	private Call call;
	private Call baseCall;
	
	private final BeanFieldGroup<Call> binder = new BeanFieldGroup<Call>(Call.class);
	
	
	
	private final CustTextField callerTextField = new CustTextField("Telefonuesi");
	private final CustTextArea addressTextArea = new CustTextArea("Adresa");
	private final CheckBox anonimCheckBox = new CheckBox("Telefonate anonime");
	private final CustComboBox dispatcherComboBox = new CustComboBox("Zona e mbulimit");
	private final CustComboBox callReasonComboBox = new CustComboBox("Arsyeja e telefonates");
	private final CustTextArea descTextArea = new CustTextArea("Pershkrimi");
	private final CustComboBox gradeComboBox = new CustComboBox("Grada e telefonates");
	
	private final  CustSaveButton saveButton = new CustSaveButton("RUAJ");
	private final  CustButton resetButton = new CustButton("Pastro");
	
	
	private final NewCallListener newCallListener;
	
	private boolean hasPreviousCall = false;
	
	
	
	public CallForm(NewCallListener newCallListener) {
		
		this.setCaption("Rregjistrimi i telefonatave");
		this.newCallListener = newCallListener;
		resetForm();
		binder.setItemDataSource(call);
		
		binder.bind(anonimCheckBox, "anonymousCaller");
		binder.bind(callerTextField, "caller");
		binder.bind(callReasonComboBox, "callReason");
		binder.bind(descTextArea, "description");
		binder.bind(addressTextArea, "address");
		binder.bind(dispatcherComboBox, "mainDispatcher");
		binder.bind(gradeComboBox, "actualGrade");
		
		
		List<CallReason> callReasonList =  ProjectLists.getCallReasonList();
		
		callReasonComboBox.setContainerDataSource(new BeanItemContainer<CallReason>(CallReason.class, callReasonList));
		callReasonComboBox.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		callReasonComboBox.setItemCaptionPropertyId("tag");
		
		List<Dispatcher> result = CommandCenterUI.getUser().getCommandCenter().getActiveDispatcherList();
		
		dispatcherComboBox.setContainerDataSource(new BeanItemContainer<Dispatcher>(Dispatcher.class, result));
		dispatcherComboBox.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		dispatcherComboBox.setItemCaptionPropertyId("name");
		
		
		List<Grade> gradeList = ProjectLists.getGradeList();
		gradeComboBox.setContainerDataSource(new BeanItemContainer<Grade>(Grade.class, gradeList));
		gradeComboBox.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		gradeComboBox.setItemCaptionPropertyId("tag");
		
		
		
			
		final FormLayout formLayout = new FormLayout();
		
		formLayout.addComponent(anonimCheckBox);
		formLayout.addComponent(callerTextField);
		formLayout.addComponent(callReasonComboBox);
		formLayout.addComponent(descTextArea);
		formLayout.addComponent(addressTextArea);
		formLayout.addComponent(dispatcherComboBox);
		formLayout.addComponent(gradeComboBox);
		
		this.addComponent(formLayout);
		
		
		final ButtonsLayout buttonsLayout = new ButtonsLayout("500px");
		resetButton.addStyleName(Runo.BUTTON_LINK);
		buttonsLayout.addComponent(resetButton);
		buttonsLayout.addComponent(saveButton);
		buttonsLayout.setComponentAlignment(saveButton, Alignment.MIDDLE_RIGHT);
		this.addComponent(buttonsLayout);
		
		
		this.addressTextArea.setWidth("300px");
		this.descTextArea.setWidth("300px");
		
		this.addStyleName("borders");
		this.setSizeUndefined();
		this.setMargin(true);
		
		this.saveButton.addClickListener(this);
		
		
	}



	@Override
	public void buttonClick(ClickEvent event) {
		
		if(event.getComponent() == saveButton)
			CustConfirmDialog.show(this.getConfirmDialogAction(), this.getUI(), "Doni te ruani telefonaten?");
		else if(event.getComponent() == resetButton){
			this.resetForm();
		}
			
	}


	public void resetForm(){
		this.hasPreviousCall = false;
		dispatcherComboBox.setEnabled(true);
		gradeComboBox.setEnabled(true);
		call = new Call(CommandCenterUI.getUser().getUsername());
		binder.setItemDataSource(call);	
	}
	
	public void resetForm(Call baseCall){
		this.hasPreviousCall = true;
		dispatcherComboBox.setEnabled(false);
		gradeComboBox.setEnabled(false);
		this.baseCall = baseCall;
		this.copyCallData(baseCall);
		binder.setItemDataSource(call);		
	}
	
	private void copyCallData(Call prevoiusCall){
		call.setCaller(prevoiusCall.getCaller());
		call.setAnonymousCaller(prevoiusCall.isAnonymousCaller());
		call.setDescription(prevoiusCall.getDescription());
		call.setAddress(prevoiusCall.getAddress());
		call.setCallReason(prevoiusCall.getCallReason());
		call.setMainDispatcher(prevoiusCall.getMainDispatcher());
		call.setActualGrade(prevoiusCall.getActualGrade());
		call.setActualStatus(prevoiusCall.getActualStatus());
		call.setRecordUser(CommandCenterUI.getUser().getUsername());
		
	}
	private ConfirmDialogAction getConfirmDialogAction(){
		ConfirmDialogAction confirm = new ConfirmDialogAction() {
			
			@Override
			public void actYes() {
				try {
					binder.commit();			
					call.setCommandCenter(CommandCenterUI.getUser().getCommandCenter());
					
					if(!hasPreviousCall){
						call.setActualStatus(CallStatusEnum.N);	
					}
					else{
						call.setBaseCall(baseCall);
					}
						
					newCallListener.addNewCall(call);

					
				} catch (CommitException e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void actNO() {
				
				
			}
		};
		
		return confirm;
	}
	
	
}
